<?php
class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
      
      $model = new Post();
      $criteria = new CDbCriteria;
      $criteria->offset = 0;
      $criteria->limit = 10;
      $criteria->order = 'post_vip DESC, post_date_update DESC';
      $models = $model->with('district','province')->findAll($criteria);
      $criteria->limit = 7;
      $criteria->order = 'news_id DESC';
      $news = News::model()->findAll($criteria);
      //CVarDumper::dump($models,10,true);die();
      $this->render('index',array(
         'model' => $model,
         'models' => $models,
         'news' => $news,
      ));
	}
   
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	   $this->layout = 'error';
		if($error=Yii::app()->errorHandler->error)
		{
		    
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

   
	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
      if(Yii::app()->user->id)
         $this->redirect('/');
      $model=new LoginForm;
      
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
      
      $this->layout = 'login';
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
   
   /**
	 * Displays the register page
	 */
	public function actionRegister()
	{
		$model=new Member;
      
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['Member']))
		{
			$model->attributes=$_POST['Member'];
			// validate user input and redirect to the previous page if valid
         $model->Birthday = date("Y-m-d", strtotime(str_replace('/', '-', $model->Birthday)));
         $model->Password = md5($model->Password);
         $model->repeatPassword = $model->Password;
         $model->MemberGroupID = 4;
         $criteria = new CDbCriteria();
         $criteria->addCondition('LoginName =:LoginName','OR');
         $criteria->addCondition('Email =:Email','OR');
         $criteria->addCondition('Mobile =:Mobile','OR');
         $criteria->params = array(
            ':LoginName' => $model->LoginName,
            ':Email' => $model->Email,
            ':Mobile' => $model->Mobile,
         );
         $check_exists = $model->find($criteria);
         if(!is_null($check_exists)){
            Yii::app()->user->setFlash('error','Tên đăng nhập, Email, hoặc SĐT bạn vừa nhập đã được đăng ký');
         }else{
            if($model->validate())
               if($model->save())
                  Yii::app()->user->setFlash('success','Đăng ký thành công');
         }
         
			
		}
      
      $this->layout = 'login';
		// display the login form
		$this->render('register',array('model'=>$model));
	}
   
   
   /**
	 * Displays the contact page
	 */
   public function actionContact(){
      
      $model = new Feedback();
      $this->layout = 'login';
      if(isset($_POST['Feedback'])){
         $model->attributes = $_POST['Feedback'];
         $model->feed_date_create = time();
         if($model->validate()){
            if($model->save()){
               Yii::app()->user->setFlash('success','Cảm ơn! Chúng tôi đã nhận được ý kiến phản hồi của bạn.');
               $model->unsetAttributes();
            }
         }
      }
		// display the login form
		$this->render('contact',array('model'=>$model));
   }
}